using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Modbus.Device;
using VncSharp;

namespace 发电机组监测;

public class Form1 : Form
{
	private static bool TaskBusy;

	private ModbusSerialMaster master;

	private Mutex mutexApp;

	private string filePath = "";

	private string FormName = "发电机组监控软件V1.0 ";

	private int FormLeft = 0;

	private int FormTop = 0;

	private string[] State = new string[19]
	{
		"停机", "手动准备", "自动模式", "", "预热延时", "启动准备", "启动延时", "盘车", "盘车延时", "盘车间隔延时",
		"启动成功安全延时", "怠速延时", "机组运行安全延时", "发电供电判断", "机组运行", "机组冷却延时", "停机延时", "停机", "停机"
	};

	private string[] FaultWarning = new string[67]
	{
		"总故障", "故障发电低电压", "故障发电高电压", "故障发电低频率", "故障发电高频率", "故障超负荷", "故障过电流", "", "故障欠速", "故障超速",
		"故障低油压", "故障高温", "故障油压低开关输入", "故障高温开关输入", "故障外部停机输入", "故障盘车失败", "故障停机失败", "故障转速信号丢失", "故障传感器1低温", "故障传感器1高温",
		"故障传感器1低油压", "故障传感器1高油压", "故障传感器1低油位", "故障传感器1高油位", "故障传感器2低温", "故障传感器2高温", "故障传感器2低油压", "故障传感器2高油压", "", "",
		"故障紧急停机", "", "总市电故障", "故障市电低电压", "故障市电高电压", "故障市电低频率", "故障市电高频率", "", "", "",
		"总警告", "警告发电低电压", "警告发电高电压", "警告发电低频率", "警告发电高频率", "警告超负荷", "警告过电流", "警告欠速", "警告超速", "警告低油压",
		"警告高温", "警告电池低电压", "警告电池高电压", "警告充电低电压", "警告燃料低", "警告传感器1低温", "警告传感器1高温", "警告传感器1低油压", "警告传感器1高油压", "警告传感器1低油位",
		"警告传感器1高油位", "警告传感器2低温", "警告传感器2高温", "警告传感器2低油压", "警告传感器2高油压", "", ""
	};

	private IContainer components = null;

	private Button button1;

	private ComboBox comboBox1;

	private Label label1;

	private TabControl tabControl1;

	private TabPage tabPage1;

	private Label label2;

	private Button button4;

	private Button button3;

	private TabPage tabPage2;

	private CheckBox checkBox2;

	private CheckBox checkBox1;

	private Button button2;

	private TextBox textBox1;

	private Label label3;

	private SerialPort serialPort1;

	private System.Windows.Forms.Timer timer1;

	private Label label4;

	private Button button5;

	private ListView listView1;

	private ColumnHeader 序号;

	private ColumnHeader 名称;

	private ColumnHeader 值;

	private ColumnHeader 单位;

	private Label label6;

	private Label label5;

	private Label label7;
    private TabPage tabPage3;
    private TabPage tabPage4;
    private VncSharp.RemoteDesktop _rd;
    private Microsoft.Web.WebView2.WinForms.WebView2 webView2;
    private Label label8;

	public Form1()
	{
		InitializeComponent();
        _rd.GetPassword = new AuthenticateDelegate(GetPassword);

    }

    private static string GetPassword()
    {
        // Do your database/file/etc. work here and return a password
        return "lenghun";
    }

    private void Form1_Load(object sender, EventArgs e)
	{

        webView2.Source = new Uri("http://sems.kgooer.com:8082/user/login");


        string currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
		string currentDirectory2 = currentDirectory.Substring(0, currentDirectory.Length - 1);
		string folderName = currentDirectory2.Substring(currentDirectory2.LastIndexOf('\\') + 1);
		mutexApp = new Mutex(initiallyOwned: false, folderName, out var blnIsRunning);
		if (!blnIsRunning)
		{
			MessageBox.Show("程序已经运行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			Environment.Exit(0);
			return;
		}
		//Text = FormName;
		filePath = currentDirectory + "config.ini";
		string[] ports = SerialPort.GetPortNames();
		ComboBox.ObjectCollection items = comboBox1.Items;
		object[] items2 = ports;
		items.AddRange(items2);
		Dictionary<string, string> iniData = ReadIniFile(filePath);
		if (iniData.TryGetValue("窗体.左边距", out var Left) && Left != "")
		{
			FormLeft = Convert.ToInt32(Left);
		}
		if (iniData.TryGetValue("窗体.上边距", out var Top) && Top != "")
		{
			FormTop = Convert.ToInt32(Top);
		}
		if (FormLeft >= 0 && FormTop >= 0)
		{
			base.Location = new Point(FormLeft, FormTop);
		}
		if (iniData.TryGetValue("发电机.名称", out var Name) && Name != "")
		{
			textBox1.Text = Name;
			//Text = FormName + textBox1.Text + " 已关闭";
		}
		if (iniData.TryGetValue("串口.自动打开", out var AutoOpen) && AutoOpen == "1")
		{
			checkBox1.Checked = true;
		}
		if (iniData.TryGetValue("巡检.自动打开", out var AutoScan) && AutoScan == "1")
		{
			checkBox2.Checked = true;
		}
		if (iniData.TryGetValue("串口.串口号", out var comx) && comx != "")
		{
			int index = comboBox1.FindStringExact(comx);
			if (index >= 0)
			{
				comboBox1.SelectedIndex = index;
				if (checkBox1.Checked)
				{
					serialPort1.PortName = comx;
					if (!serialPort1.IsOpen)
					{
						try
						{
							serialPort1.Open();
							master = ModbusSerialMaster.CreateRtu(serialPort1);
							master.Transport.ReadTimeout = 5000;
							master.Transport.WriteTimeout = 5000;
							button1.Enabled = false;
							button2.Enabled = true;
							button3.Enabled = true;
							button4.Enabled = true;
							button5.Enabled = true;
							if (checkBox2.Checked)
							{
								timer1.Enabled = true;
							}
							//Text = FormName + textBox1.Text + " 已打开";
						}
						catch
						{
						}
					}
				}
			}
		}
		ListViewItem[] item = new ListViewItem[15];
		for (int i = 0; i < item.Length; i++)
		{
			item[i] = new ListViewItem();
		}
		item[0].Text = "1";
		item[0].SubItems.Add("蓄电池电压");
		item[0].SubItems.Add("0.0");
		item[0].SubItems.Add("V");
		item[1].Text = "2";
		item[1].SubItems.Add("发动机水温");
		item[1].SubItems.Add("0");
		item[1].SubItems.Add("℃");
		item[2].Text = "3";
		item[2].SubItems.Add("发动机转速");
		item[2].SubItems.Add("0");
		item[2].SubItems.Add("r/min");
		item[3].Text = "4";
		item[3].SubItems.Add("发动机机油压力");
		item[3].SubItems.Add("0.0");
		item[3].SubItems.Add("bar");
		item[4].Text = "5";
		item[4].SubItems.Add("发动机燃油位");
		item[4].SubItems.Add("0");
		item[4].SubItems.Add("%");
		item[5].Text = "6";
		item[5].SubItems.Add("发电频率");
		item[5].SubItems.Add("0.0");
		item[5].SubItems.Add("HZ");
		item[6].Text = "7";
		item[6].SubItems.Add("发电电压U相");
		item[6].SubItems.Add("0");
		item[6].SubItems.Add("V");
		item[7].Text = "8";
		item[7].SubItems.Add("发电电压V相");
		item[7].SubItems.Add("0");
		item[7].SubItems.Add("V");
		item[8].Text = "9";
		item[8].SubItems.Add("发电电压W相");
		item[8].SubItems.Add("0");
		item[8].SubItems.Add("V");
		item[9].Text = "10";
		item[9].SubItems.Add("发电电压UV");
		item[9].SubItems.Add("0");
		item[9].SubItems.Add("V");
		item[10].Text = "11";
		item[10].SubItems.Add("发电电压VW");
		item[10].SubItems.Add("0");
		item[10].SubItems.Add("V");
		item[11].Text = "12";
		item[11].SubItems.Add("发电电压WU");
		item[11].SubItems.Add("0");
		item[11].SubItems.Add("V");
		item[12].Text = "13";
		item[12].SubItems.Add("输出电流U相");
		item[12].SubItems.Add("0");
		item[12].SubItems.Add("A");
		item[13].Text = "14";
		item[13].SubItems.Add("输出电流V相");
		item[13].SubItems.Add("0");
		item[13].SubItems.Add("A");
		item[14].Text = "15";
		item[14].SubItems.Add("输出电流W相");
		item[14].SubItems.Add("0");
		item[14].SubItems.Add("A");
		listView1.BeginUpdate();
		ListViewItem[] array = item;
		foreach (ListViewItem itemx in array)
		{
			listView1.Items.Add(itemx);
		}
		listView1.EndUpdate();
	}


    private void button1_Click(object sender, EventArgs e)
	{
		if (comboBox1.SelectedIndex < 0)
		{
			return;
		}
		button1.Enabled = false;
		button2.Enabled = true;
		button3.Enabled = true;
		button4.Enabled = true;
		button5.Enabled = true;
		if (comboBox1.SelectedIndex < 0)
		{
			return;
		}
		serialPort1.PortName = comboBox1.SelectedItem.ToString();
		if (serialPort1.IsOpen)
		{
			return;
		}
		try
		{
			serialPort1.Open();
			master = ModbusSerialMaster.CreateRtu(serialPort1);
			master.Transport.ReadTimeout = 5000;
			master.Transport.WriteTimeout = 5000;
			if (checkBox2.Checked)
			{
				timer1.Enabled = true;
			}
			//Text = FormName + textBox1.Text + " 已打开";
		}
		catch
		{
		}
	}

	private void button2_Click(object sender, EventArgs e)
	{
		timer1.Enabled = false;
		if (serialPort1.IsOpen)
		{
			master.Dispose();
			serialPort1.Close();
			//Text = FormName + textBox1.Text + " 已关闭";
		}
		button2.Enabled = false;
		button1.Enabled = true;
		button3.Enabled = false;
		button4.Enabled = false;
		button5.Enabled = false;
	}

	private static Dictionary<string, string> ReadIniFile(string filePath)
	{
		Dictionary<string, string> iniData = new Dictionary<string, string>();
		try
		{
			using StreamReader reader = new StreamReader(filePath);
			string currentSection = "";
			string line;
			while ((line = reader.ReadLine()) != null)
			{
				line = line.Trim();
				if (line.StartsWith("[") && line.EndsWith("]"))
				{
					currentSection = line.Substring(1, line.Length - 2);
				}
				else if (!string.IsNullOrEmpty(line) && !line.StartsWith(";"))
				{
					int equalsIndex = line.IndexOf('=');
					if (equalsIndex > 0)
					{
						string key = line.Substring(0, equalsIndex).Trim();
						string value = line.Substring(equalsIndex + 1).Trim();
						string sectionKey = currentSection + "." + key;
						iniData[sectionKey] = value;
					}
				}
			}
		}
		catch
		{
		}
		return iniData;
	}

	private static void WriteIniFile(string filePath, Dictionary<string, string> iniData)
	{
		using StreamWriter writer = new StreamWriter(filePath);
		string currentSection = "";
		foreach (KeyValuePair<string, string> kvp in iniData)
		{
			string[] parts = kvp.Key.Split('.');
			if (parts.Length > 1)
			{
				string section = parts[0];
				string key = parts[1];
				if (section != currentSection)
				{
					writer.WriteLine("[" + section + "]");
					currentSection = section;
				}
				writer.WriteLine(key + " = " + kvp.Value);
			}
		}
	}

	private void Form1_FormClosed(object sender, FormClosedEventArgs e)
	{

        //try
        //{
        //    _rd.Disconnect();
        //}
        //catch
        //{
        //}


        timer1.Enabled = false;
		if (serialPort1.IsOpen)
		{
			master.Dispose();
			serialPort1.Close();
		}
		Dictionary<string, string> iniData = new Dictionary<string, string>();
		if (comboBox1.SelectedIndex >= 0)
		{
			iniData["串口.串口号"] = comboBox1.SelectedItem.ToString();
		}
		else
		{
			iniData["串口.串口号"] = "COM1";
		}
		if (textBox1.Text != "")
		{
			iniData["发电机.名称"] = textBox1.Text;
		}
		else
		{
			iniData["发电机.名称"] = "发电机1";
		}
		if (checkBox1.Checked)
		{
			iniData["串口.自动打开"] = "1";
		}
		else
		{
			iniData["串口.自动打开"] = "0";
		}
		if (checkBox2.Checked)
		{
			iniData["巡检.自动打开"] = "1";
		}
		else
		{
			iniData["巡检.自动打开"] = "0";
		}
		iniData["窗体.左边距"] = FormLeft.ToString();
		iniData["窗体.上边距"] = FormTop.ToString();
		WriteIniFile(filePath, iniData);
	}

	private void button4_Click(object sender, EventArgs e)
	{
		if (serialPort1.IsOpen)
		{
			ushort[] reg = new ushort[3] { 0, 1, 0 };
			try
			{
				master.WriteMultipleRegisters(1, 0, reg);
			}
			catch
			{
			}
		}
	}

	private void button3_Click(object sender, EventArgs e)
	{
		if (serialPort1.IsOpen)
		{
			ushort[] reg = new ushort[3] { 1, 0, 0 };
			try
			{
				master.WriteMultipleRegisters(1, 0, reg);
			}
			catch
			{
			}
		}
	}

	private void timer1_Tick(object sender, EventArgs e)
	{
		if (TaskBusy)
		{
			return;
		}
		Task task = new Task(delegate
		{
			TaskBusy = true;
			if (serialPort1.IsOpen)
			{
				try
				{
					ushort[] reg = master.ReadHoldingRegisters(1, 100, 72);
					MethodInvoker method = delegate
					{
						listView1.Items[0].SubItems[2].Text = ((double)(int)reg[18] / 10.0).ToString("0.0");
						listView1.Items[1].SubItems[2].Text = reg[19].ToString();
						listView1.Items[2].SubItems[2].Text = reg[20].ToString();
						listView1.Items[3].SubItems[2].Text = ((double)(int)reg[21] / 10.0).ToString("0.0");
						listView1.Items[4].SubItems[2].Text = reg[22].ToString();
						listView1.Items[5].SubItems[2].Text = ((double)(int)reg[9] / 10.0).ToString("0.0");
						listView1.Items[6].SubItems[2].Text = reg[0].ToString();
						listView1.Items[7].SubItems[2].Text = reg[1].ToString();
						listView1.Items[8].SubItems[2].Text = reg[2].ToString();
						listView1.Items[9].SubItems[2].Text = reg[3].ToString();
						listView1.Items[10].SubItems[2].Text = reg[4].ToString();
						listView1.Items[11].SubItems[2].Text = reg[5].ToString();
						listView1.Items[12].SubItems[2].Text = reg[6].ToString();
						listView1.Items[13].SubItems[2].Text = reg[7].ToString();
						listView1.Items[14].SubItems[2].Text = reg[8].ToString();
						label5.Text = State[reg[27]];
						if (reg[42] >= 200)
						{
							label6.Text = FaultWarning[reg[42] - 200 + 40];
						}
						else if (reg[42] >= 100)
						{
							label6.Text = FaultWarning[reg[42] - 100];
						}
						else
						{
							label6.Text = "";
						}
						label7.Text = "读取成功！";
						label8.Text = reg[28].ToString("0s");
					};
					BeginInvoke(method);
				}
				catch
				{
					MethodInvoker method2 = delegate
					{
						label7.Text = "读取失败！";
					};
					BeginInvoke(method2);
				}
			}
			TaskBusy = false;
		});
		task.Start();
	}

	private void textBox1_TextChanged(object sender, EventArgs e)
	{
		//Text = FormName + textBox1.Text;
	}

	private void checkBox1_CheckedChanged(object sender, EventArgs e)
	{
		if (!checkBox1.Checked || serialPort1.IsOpen || comboBox1.SelectedIndex < 0)
		{
			return;
		}
		serialPort1.PortName = comboBox1.SelectedItem.ToString();
		try
		{
			serialPort1.Open();
			master = ModbusSerialMaster.CreateRtu(serialPort1);
			master.Transport.ReadTimeout = 5000;
			master.Transport.WriteTimeout = 5000;
			button1.Enabled = false;
			button2.Enabled = true;
			button3.Enabled = true;
			button4.Enabled = true;
			button5.Enabled = true;
			if (checkBox2.Checked)
			{
				timer1.Enabled = true;
			}
		}
		catch
		{
		}
	}

	private void checkBox2_CheckedChanged(object sender, EventArgs e)
	{
		if (checkBox2.Checked)
		{
			if (serialPort1.IsOpen)
			{
				timer1.Enabled = true;
			}
		}
		else
		{
			timer1.Enabled = false;
		}
	}

	private void Form1_FormClosing(object sender, FormClosingEventArgs e)
	{
		if (TaskBusy)
		{
			e.Cancel = true;
			return;
		}
		FormLeft = base.Location.X;
		FormTop = base.Location.Y;
	}

	private void button5_Click(object sender, EventArgs e)
	{
		if (serialPort1.IsOpen)
		{
			ushort[] reg = new ushort[3] { 0, 0, 1 };
			try
			{
				master.WriteMultipleRegisters(1, 0, reg);
			}
			catch
			{
			}
		}
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
            this.components = new System.ComponentModel.Container();
            this.button1 = new System.Windows.Forms.Button();
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage4 = new System.Windows.Forms.TabPage();
            this.webView2 = new Microsoft.Web.WebView2.WinForms.WebView2();
            this.tabPage3 = new System.Windows.Forms.TabPage();
            this._rd = new VncSharp.RemoteDesktop();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.label8 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.button5 = new System.Windows.Forms.Button();
            this.listView1 = new System.Windows.Forms.ListView();
            this.序号 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.名称 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.值 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.单位 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.label4 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.button4 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.checkBox2 = new System.Windows.Forms.CheckBox();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.button2 = new System.Windows.Forms.Button();
            this.serialPort1 = new System.IO.Ports.SerialPort(this.components);
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.tabControl1.SuspendLayout();
            this.tabPage4.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.webView2)).BeginInit();
            this.tabPage3.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.tabPage2.SuspendLayout();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(280, 53);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(68, 20);
            this.button1.TabIndex = 0;
            this.button1.Text = "打开";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // comboBox1
            // 
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(189, 53);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(85, 20);
            this.comboBox1.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(130, 57);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(53, 12);
            this.label1.TabIndex = 2;
            this.label1.Text = "串口列表";
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabPage4);
            this.tabControl1.Controls.Add(this.tabPage3);
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabControl1.Location = new System.Drawing.Point(0, 0);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(1563, 814);
            this.tabControl1.TabIndex = 3;
            // 
            // tabPage4
            // 
            this.tabPage4.Controls.Add(this.webView2);
            this.tabPage4.Location = new System.Drawing.Point(4, 22);
            this.tabPage4.Name = "tabPage4";
            this.tabPage4.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage4.Size = new System.Drawing.Size(1555, 788);
            this.tabPage4.TabIndex = 3;
            this.tabPage4.Text = "能耗统计";
            this.tabPage4.UseVisualStyleBackColor = true;
            // 
            // webView2
            // 
            this.webView2.AllowExternalDrop = true;
            this.webView2.CreationProperties = null;
            this.webView2.DefaultBackgroundColor = System.Drawing.Color.White;
            this.webView2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.webView2.Location = new System.Drawing.Point(3, 3);
            this.webView2.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
            this.webView2.Name = "webView2";
            this.webView2.Size = new System.Drawing.Size(1549, 782);
            this.webView2.TabIndex = 1;
            this.webView2.ZoomFactor = 1D;
            this.webView2.NavigationCompleted += new System.EventHandler<Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs>(this.webView2_NavigationCompleted);
            this.webView2.WebMessageReceived += new System.EventHandler<Microsoft.Web.WebView2.Core.CoreWebView2WebMessageReceivedEventArgs>(this.webView2_WebMessageReceived);
            // 
            // tabPage3
            // 
            this.tabPage3.Controls.Add(this._rd);
            this.tabPage3.Location = new System.Drawing.Point(4, 22);
            this.tabPage3.Name = "tabPage3";
            this.tabPage3.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage3.Size = new System.Drawing.Size(1555, 788);
            this.tabPage3.TabIndex = 2;
            this.tabPage3.Text = "EMS管理";
            this.tabPage3.UseVisualStyleBackColor = true;
            // 
            // _rd
            // 
            this._rd.AutoScroll = true;
            this._rd.AutoScrollMinSize = new System.Drawing.Size(608, 427);
            this._rd.Dock = System.Windows.Forms.DockStyle.Fill;
            this._rd.Location = new System.Drawing.Point(3, 3);
            this._rd.Name = "_rd";
            this._rd.Size = new System.Drawing.Size(1549, 782);
            this._rd.TabIndex = 1;
            this._rd.ConnectComplete += new VncSharp.ConnectCompleteHandler(this._rd_ConnectComplete);
            // 
            // tabPage1
            // 
            this.tabPage1.Controls.Add(this.label8);
            this.tabPage1.Controls.Add(this.label7);
            this.tabPage1.Controls.Add(this.label6);
            this.tabPage1.Controls.Add(this.label5);
            this.tabPage1.Controls.Add(this.button5);
            this.tabPage1.Controls.Add(this.listView1);
            this.tabPage1.Controls.Add(this.label4);
            this.tabPage1.Controls.Add(this.label2);
            this.tabPage1.Controls.Add(this.button4);
            this.tabPage1.Controls.Add(this.button3);
            this.tabPage1.Location = new System.Drawing.Point(4, 22);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage1.Size = new System.Drawing.Size(1555, 788);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "柴油发电机";
            this.tabPage1.UseVisualStyleBackColor = true;
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(398, 629);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(17, 12);
            this.label8.TabIndex = 8;
            this.label8.Text = "0s";
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(398, 652);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(0, 12);
            this.label7.TabIndex = 7;
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(128, 652);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(0, 12);
            this.label6.TabIndex = 6;
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(127, 629);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(0, 12);
            this.label5.TabIndex = 5;
            // 
            // button5
            // 
            this.button5.Enabled = false;
            this.button5.Location = new System.Drawing.Point(398, 677);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(113, 44);
            this.button5.TabIndex = 4;
            this.button5.Text = "自动";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.button5_Click);
            // 
            // listView1
            // 
            this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.序号,
            this.名称,
            this.值,
            this.单位});
            this.listView1.FullRowSelect = true;
            this.listView1.GridLines = true;
            this.listView1.HideSelection = false;
            this.listView1.Location = new System.Drawing.Point(34, 14);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(704, 585);
            this.listView1.TabIndex = 3;
            this.listView1.UseCompatibleStateImageBehavior = false;
            this.listView1.View = System.Windows.Forms.View.Details;
            // 
            // 序号
            // 
            this.序号.Text = "序号";
            this.序号.Width = 44;
            // 
            // 名称
            // 
            this.名称.Text = "名称";
            this.名称.Width = 151;
            // 
            // 值
            // 
            this.值.Text = "值";
            this.值.Width = 128;
            // 
            // 单位
            // 
            this.单位.Text = "单位";
            this.单位.Width = 56;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(45, 652);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(77, 12);
            this.label4.TabIndex = 2;
            this.label4.Text = "故障警告信息";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(45, 629);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(65, 12);
            this.label2.TabIndex = 1;
            this.label2.Text = "控制器状态";
            // 
            // button4
            // 
            this.button4.Enabled = false;
            this.button4.Location = new System.Drawing.Point(235, 677);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(113, 44);
            this.button4.TabIndex = 0;
            this.button4.Text = "启动";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // button3
            // 
            this.button3.Enabled = false;
            this.button3.Location = new System.Drawing.Point(77, 677);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(113, 44);
            this.button3.TabIndex = 0;
            this.button3.Text = "停止";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // tabPage2
            // 
            this.tabPage2.Controls.Add(this.textBox1);
            this.tabPage2.Controls.Add(this.label3);
            this.tabPage2.Controls.Add(this.checkBox2);
            this.tabPage2.Controls.Add(this.checkBox1);
            this.tabPage2.Controls.Add(this.button2);
            this.tabPage2.Controls.Add(this.button1);
            this.tabPage2.Controls.Add(this.label1);
            this.tabPage2.Controls.Add(this.comboBox1);
            this.tabPage2.Location = new System.Drawing.Point(4, 22);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage2.Size = new System.Drawing.Size(1555, 788);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "端口配置";
            this.tabPage2.UseVisualStyleBackColor = true;
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(201, 89);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(73, 21);
            this.textBox1.TabIndex = 7;
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(130, 94);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(65, 12);
            this.label3.TabIndex = 6;
            this.label3.Text = "发电机名称";
            // 
            // checkBox2
            // 
            this.checkBox2.AutoSize = true;
            this.checkBox2.Location = new System.Drawing.Point(276, 174);
            this.checkBox2.Name = "checkBox2";
            this.checkBox2.Size = new System.Drawing.Size(72, 16);
            this.checkBox2.TabIndex = 5;
            this.checkBox2.Text = "自动读取";
            this.checkBox2.UseVisualStyleBackColor = true;
            this.checkBox2.CheckedChanged += new System.EventHandler(this.checkBox2_CheckedChanged);
            // 
            // checkBox1
            // 
            this.checkBox1.AutoSize = true;
            this.checkBox1.Location = new System.Drawing.Point(147, 174);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(72, 16);
            this.checkBox1.TabIndex = 4;
            this.checkBox1.Text = "自动打开";
            this.checkBox1.UseVisualStyleBackColor = true;
            this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
            // 
            // button2
            // 
            this.button2.Enabled = false;
            this.button2.Location = new System.Drawing.Point(354, 53);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(68, 20);
            this.button2.TabIndex = 3;
            this.button2.Text = "关闭";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // timer1
            // 
            this.timer1.Interval = 250;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1563, 814);
            this.Controls.Add(this.tabControl1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Name = "Form1";
            this.Text = "能源管理面板";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
            this.Load += new System.EventHandler(this.Form1_Load);
            this.Shown += new System.EventHandler(this.Form1_Shown);
            this.tabControl1.ResumeLayout(false);
            this.tabPage4.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.webView2)).EndInit();
            this.tabPage3.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.tabPage1.PerformLayout();
            this.tabPage2.ResumeLayout(false);
            this.tabPage2.PerformLayout();
            this.ResumeLayout(false);

	}

    private void Form1_Shown(object sender, EventArgs e)
    {
        try
        {
            _rd.Connect("192.168.110.10", 0, false, false);
        }
        catch (Exception ex)
        {
            MessageBox.Show(this, ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void _rd_ConnectComplete(object sender, ConnectEventArgs e)
    {
        //ClientSize = new Size(e.DesktopWidth, e.DesktopHeight);
        //Text = e.DesktopName;

        _rd.Focus();
    }

    private void webView2_WebMessageReceived(object sender, Microsoft.Web.WebView2.Core.CoreWebView2WebMessageReceivedEventArgs e)
    {
        var data = e.WebMessageAsJson;
        webView2.CoreWebView2.PostWebMessageAsJson(data);
    }

    private async void webView2_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
    {
        await webView2.CoreWebView2.ExecuteScriptAsync($"document.querySelector('#tehForm > div:nth-child(1) > input[type=text]').value = 'hwg215';");
        await webView2.CoreWebView2.ExecuteScriptAsync($"document.querySelector('#tehForm > div:nth-child(2) > input[type=password]').value = '215005';");
        Thread.Sleep(500);
        await webView2.CoreWebView2.ExecuteScriptAsync($"document.querySelector('#tehForm > div:nth-child(3) > button').click();");

    }
}
